/*
Stringlist Library v0.1.a
Copyright (c) 2011, Sulaiman A. Mustafa
All rights reserved.

   Redistribution and use in source and binary forms, with or without 
   modification, are permitted provided that the following conditions 
   are met:

     Redistributions of source code must retain the above copyright 
     notice, this list of conditions and the following disclaimer.
     
     Redistributions in binary form must reproduce the above 
     copyright notice, this list of conditions and the following 
     disclaimer in the documentation and/or other materials provided 
     with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Things to implement in the future (if we live that long):
	1. Might change string type from char * to a structure with string length
		to improve performance.

Example:

	int main(int argc, char **argv){
		struct stringlist list;
		char *out;

		initalize_stringlist(&list); //required
	
		add_string_to_stringlist(&list, "fisrt string");
		add_string_to_stringlist(&list, "second string");
		add_string_to_stringlist(&list, "third string");

		out=get_next_string_from_stringlist(&list, 0);
	
		if(out)
			puts(out);
		else 
			puts("(NULL)");

		return 0;
	}

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sllib.h"

void initialize_stringlist(struct stringlist *list){
	list->list=NULL;
	list->count=0;
}

char *initialize_string(const char* string){
	char *buffer;
	if (!string){
		fprintf(stderr, "initialize_string: Null argument!\n");
		exit(1);		
	}
	if(!(buffer=(char *) malloc(sizeof(char)*(strlen(string)+1)))){
		fprintf(stderr, "initialize_string: Out of memory!");
		exit(1);
	}
	strcpy(buffer, string);
	return buffer;
}

void add_string_to_stringlist(struct stringlist *list, char *string){
	int length=(strlen(string)+1);
	++list->count;
	
	if(!(list->list=(char **)realloc(list->list, sizeof(char *)*list->count))){
		fprintf(stderr, "add_string_to_stringlist: Out of memory!");
		exit(100);
	}
	if(!(*(list->list+list->count-1)=(char *) malloc(sizeof(char)*length))){
		fprintf(stderr, "add_string_to_stringlist: Out of memory!");
		exit(101);
	}
	strncpy(*(list->list+list->count-1), string, length);
}

void free_stringlist(struct stringlist *list){
	int i;
	if(!(list->count || list->list)) return;
	for (i=0; i<list->count; ++i) free_string(*(list->list+i));
	free(list->list);
	list->list=NULL;
	list->count=0;
}
char* get_next_string_from_stringlist(struct stringlist *list, int reset){
	static int count = 0;
	if (reset) count = 0;
	if (count == list->count) return NULL;
	else return *(list->list+count++);
}
void print_stringlist(struct stringlist *list){
	int i;
	for (i = 0; i<list->count; ++i){
		puts(*(list->list+i));
	}
}

char *append_string(char *dest, const char *src){
		int size=strlen(dest)+strlen(src)+1;
		SLLIB_STRETCH_STRING_MACRO(dest, size);
		strcat(dest, src);
		return dest;
}
char* free_string(char *string){
	if(string) free(string);
	return NULL;
}
void add_sllib_string_to_stringlist(struct stringlist *list, char *string){
	++list->count;
	
	if(!(list->list=(char **)realloc(list->list, sizeof(char *)*list->count))){
		fprintf(stderr, "add_sllib_string_to_stringlist: Out of memory!");
		exit(100);
	}
	*(list->list+list->count-1)=string;
}

/*BETA*/
char *replace_char_with_string(char *dest, char which, char *what){
	int i, mark=0, diff, dest_size=strlen(dest);
	char *buffer = initialize_string(""),	*result = initialize_string("");
	for(i=0;i<dest_size;++i){
		if(dest[i]==which){
			diff=i-mark;
			SLLIB_STRETCH_STRING_MACRO(buffer, diff+1);
			strncpy(buffer, (dest+mark), diff);
			buffer[diff]='\0';
			result=append_string(result, buffer);
			result=append_string(result, what);
			mark=i+1;
		}
	}
	result=append_string(result, dest+mark);
	return result;
}



